<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
$return['ischecklogin']=$this->model('member')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'index';
$areasetting=$this->plugin_setting('areasetting');
if($_RQ['op']=='index'){
	//还不是分销商，跳转申请页
	$return['apply']=$this->get('area_store',array('mid'=>$_SESSION['uid']));
	$return['member']=$this->get('member',array('mid'=>$_SESSION['uid']));
	$return['become']=$areasetting['become'];
	$return['page_setting']=$this->plugin_setting('areapage');
	$return['apply_setting']=$this->plugin_setting('areaapply');
	if(!$areasetting['no_indexurl']){
		$return['index_url']=$areasetting['index_url']?:'jy_storeping/index/';
	}
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));	
	$return['item']['oldmobile']=$return['item']['mobile'];
	$setting=uni_setting('register');
	$return['item']['login_type']=$setting['login_type'];
	$return['item']['area_type']=$return['item']['area_type']?:'4';
	$return['agent_level']=$areasetting['level'];
	$return['stores']=pdo_getall('jy_weishop_store',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['stores'][]=array('id'=>0,'title'=>'暂无可绑定门店(申请后开新店)');
	$return['item']['storeid']=$return['stores'][0]['id'];
	$return['province']=cfc('tools')->pcd();
	$return['area_types']=$this->area_types;
	set_html('title',$return['apply_setting']['html_title']?:'申请入驻');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if($post['storeid']){
		$isadd=$this->get('area_store',array('storeid'=>$post['storeid'],'mid'=>$_SESSION['uid']));
		if($isadd['id']){
			exi('该门店已申请代理，不能重复申请！','error');
		}
	}
	if(!$post['storeid'] && !$post['title']){
		exi('请输入门店名称！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($post['login_type']>0 && $post['oldmobile']!=$post['mobile'] && !check_sms_code($post['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($post['mobile']!=$oldmobile){
			exi('手机验证码错误！','error');
		}
	}
	//代理地区整理
	if(!$post['area'] || !$post['area']['province']){
		exi('请选择代理地区！','error');
	}
	$area_code=$this->model('area')->get_area_code($post['area']);
	$this->model('area')->apply_agent($_SESSION['uid'],$area_code,$post);
	exi('申请成功！请耐心等待管理员审核！','','/store.apply/');
}
